package com.liujianlei.forum_system.controller;

import com.liujianlei.forum_system.common.AppResult;
import com.liujianlei.forum_system.common.ResultCode;
import com.liujianlei.forum_system.config.AppConfig;
import com.liujianlei.forum_system.exception.ApplicationException;
import com.liujianlei.forum_system.model.Article;
import com.liujianlei.forum_system.model.User;
import com.liujianlei.forum_system.service.IArticleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

/**
 * @author tongchen
 * @create 2023-06-11 20:43
 */
@RestController
@Slf4j
@Api(tags = "文章接口")
@RequestMapping("/article")
public class ArticleController {
    @Resource
   private IArticleService articleService;
    @GetMapping("/getAllByBoardId")
    @ApiOperation("查询文章信息的方法")
    public AppResult getAll( @ApiParam(value = "板块id") @RequestParam(required = false) Long boardId){
        //根据是否存在参数来判断查看何种数据
        List<Article> articles = null;
        //判断参数是否为空
        if(boardId==null){
            //查询所有数据
             articles = articleService.selectAll();
             log.info("查询所有数据！");
             return AppResult.success("所有数据查询成功",articles);
        }
        //查询编号的数据
         articles= articleService.selectByBoardId(boardId);
        //打印日志信息
        log.info("当前板块的文章信息查询成功");
        if(articles==null){
            //建立一个空list
            articles=new ArrayList<>();
        }
        return AppResult.success("板块文章信息查询成功",articles);
    }
    @GetMapping("/getAllByUserId")
    @ApiOperation("按照用户id查找文章信息")
    public AppResult selectByUserId(@ApiParam("用户id")@NonNull @RequestParam Long userId){
        List<Article> articles = articleService.selectByUserId(userId);
        return AppResult.success("按照用户id查找文章信息成功",articles);
    }
    @PostMapping("/create")
    @ApiOperation("新增文章的接口")
    public AppResult createArticle(HttpServletRequest request, @RequestParam@ApiParam(value = "文章标题")@NonNull String title,@RequestParam@ApiParam(value = "文章内容")@NonNull String content,@RequestParam@ApiParam(value = "文章板块")@NonNull Long boardId){
        //在session中获取文章信息
        HttpSession session = request.getSession(false);
        if(session==null){
            //报错并记录日志
            log.error(ResultCode.USER_NOT_LOG.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.USER_NOT_LOG));
        }
        User loginUser =(User) session.getAttribute(AppConfig.USER_SESSION);
        if(loginUser==null){
            //报错并记录日志
            log.error(ResultCode.USER_NOT_LOG.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.USER_NOT_LOG));
        }
        if(loginUser.getState()==1){
            log.error(ResultCode.FAILED_USER_BANNED.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_USER_BANNED));
        }
        //创建文章
        Article article = new Article();
        article.setContent(content);
        article.setBoardId(boardId);
        article.setTitle(title);
        article.setUserId(loginUser.getId());
        //直接调用service
        articleService.insertArticle(article);
        //返回结果
        return AppResult.success("新增文章成功");
    }
    @GetMapping("/details")
    @ApiOperation("根据文章id访问文章")
    public AppResult<Article>getById(HttpServletRequest request,@RequestParam@NonNull@ApiParam(value = "文章id") Long id){
        Article article = articleService.selectDetailById(id);
        //获取登录用户id
        HttpSession session = request.getSession(false);
        User loginUser = (User) session.getAttribute(AppConfig.USER_SESSION);
        if(loginUser.getId()==article.getUserId()){
            article.setIsOwn(true);
        }
        return AppResult.success("文章查看成功",article);
    }
    @PostMapping("/modify")
    @ApiOperation("文章信息的修改")
    public AppResult modify(HttpServletRequest request,@RequestParam(required = false)@ApiParam(value = "帖子标题") String title,@RequestParam@ApiParam(value = "帖子内容")@NonNull String content,@RequestParam@ApiParam(value = "帖子id")@NonNull Long id){
        //用户是否登录
        HttpSession session = request.getSession(false);
        if(session==null){
            //报错并记录日志
            log.error(ResultCode.USER_NOT_LOG.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.USER_NOT_LOG));
        }
        User loginUser = (User)session.getAttribute(AppConfig.USER_SESSION);
        if(loginUser==null){
            //报错并记录日志
            log.error(ResultCode.USER_NOT_LOG.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.USER_NOT_LOG));
        }
        //用户状态校验
        if(loginUser.getState()==1){
            log.error(ResultCode.FAILED_USER_BANNED.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_USER_BANNED));
        }
        //判断当前用户是否是文章发布者
        Article article = articleService.selectDetailById(id);
        log.info(article.toString());
        if(article==null){
            //数据无效直接报错
            log.error(ResultCode.FAILED_NOT_EXISTS.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_NOT_EXISTS));
        }
        if(article.getUserId()!=loginUser.getId()){
            log.error(ResultCode.FAILED_UNAUTHORIZED.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_UNAUTHORIZED));
        }
        //校验文章状态
        if(article.getState()==1||article.getDeleteState()==1){
            log.error("帖子状态异常，deleteState="+article.getDeleteState()+"state="+article.getState());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        //修改文章
        articleService.modify(title, content, id);
        log.info("文章修改成功，文章id为"+id);
        //返回结果
        return AppResult.success("文章信息修改成功!");

    }
    @ApiOperation("帖子点赞")
    @PostMapping("/thumbsUp")
    public AppResult thumbsUp(HttpServletRequest request,@NonNull@RequestParam@ApiParam("文章id") Long id){
        HttpSession session = request.getSession(false);
        if(session==null){
            //报错并记录日志
            log.error(ResultCode.USER_NOT_LOG.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.USER_NOT_LOG));
        }
        User loginUser = (User)session.getAttribute(AppConfig.USER_SESSION);
        if(loginUser==null){
            //报错并记录日志
            log.error(ResultCode.USER_NOT_LOG.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.USER_NOT_LOG));
        }
        //校验用户的状态
        if(loginUser.getState()==1){
            log.error("用户状态为禁言状态");
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_USER_BANNED));
        }
        //调用service处理
        articleService.thumbsUpById(id);
        return AppResult.success("用户点赞成功");
    }
    @GetMapping("/delete")
    @ApiOperation("删除帖子api")
    public AppResult deleteById(HttpServletRequest request,@NonNull@RequestParam@ApiParam("帖子id") Long id){
        //测试用户登录状态
        HttpSession session = request.getSession(false);
        if(session==null){
            //报错并记录日志
            log.error(ResultCode.USER_NOT_LOG.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.USER_NOT_LOG));
        }
        User loginUser = (User)session.getAttribute(AppConfig.USER_SESSION);
        if(loginUser==null){
            //报错并记录日志
            log.error(ResultCode.USER_NOT_LOG.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.USER_NOT_LOG));
        }
        //判断用户状态
        if(loginUser.getState()==1){
            log.error("用户状态为禁言状态");
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_USER_BANNED));
        }
        //判断用户是否有权进行删除文章
        Article article = articleService.selectDetailById(id);
        if(article==null||article.getUserId()!= loginUser.getId()){
            log.error("该用户无权进行删除操作");
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_UNAUTHORIZED));
        }
        //有权进行删除文章即可
        articleService.deleteArticle(id);
        //记录删除成功信息
        log.info("帖子删除成功");
        return AppResult.success("日志删除成功");
    }

}
